package ar.unc.famaf.pln.giveselector.filters;

import static java.lang.Math.PI;
import static java.lang.Math.abs;

import java.util.*;

import ar.unc.famaf.pln.giveselector.context.*;

/**
 * An OrientationFilter filters out utterances that contain orientation keywords but whose context orientation does not
 * match the current context orientation.
 * @author Alexandre Denis
 */
public class OrientationFilter extends LexicalFilter
{
	private double ORIENTATION_THRESHOLD = PI / 2;
	private static String[] orientationWords = new String[] { "left", "right", "straight", "turn", "in front", "that" };


	/**
	 * Creates a new OrientationFilter.
	 */
	public OrientationFilter()
	{
		super(orientationWords);
	}


	@Override
	public Set<Utterance> filter(Set<Utterance> utterances, Context context)
	{
		System.out.println(getClass().getSimpleName() + " Orientation Filter input: " + utterancesToString(utterances));
		Set<Utterance> utts = new HashSet<Utterance>(utterances);
		for(Iterator<Utterance> it = utts.iterator(); it.hasNext();)
		{
			Utterance utt = it.next();
			if (isFiltered(utt) && !matchOrientation(context, utt.getContext()))
				it.remove();
		}
		System.out.println(getClass().getSimpleName() + " Orientation Filter output: " + utterancesToString(utts));
		return utts;
	}


	/**
	 * Tests if the two contexts orientation is similar up to a given treshold.
	 */
	private boolean matchOrientation(Context c1, Context c2)
	{
		return abs(c1.getOrientation().getAngleTo(c2.getOrientation())) < ORIENTATION_THRESHOLD;
	}
}
